const express=require('express')
const router= express.Router();
const db=require('../../utils/db')

// 获取菜品类别
router.get('/getfoodcategory',function(req,res){
    let mysql='SELECT menu_category FROM menu_file GROUP BY menu_category'
    db(mysql,null,(err,data)=>{
        if(err){
            return res.json({
                code: 500,
                msg: '查询菜品类别失败，请稍后重试'
            })
        }
        res.json({
            code: 200,
            msg: '查询菜品类别成功',
            data: data
        })
    })
})

// 获取菜品类别对应数据
router.get('/getfood',function(req,res){
    let mysql='SELECT * FROM menu_file WHERE menu_category=?'
    if(req.query.foodNum){
        mysql+=` AND menu_num like ?`
    }
    db(mysql,[req.query.category,'%'+req.query.foodNum+'%'],(err,data)=>{
        if(err){
            return res.json({
                code: 500,
                msg: '查询菜品失败，请稍后重试'
            })
        }
        res.json({
            code: 200,
            msg: '查询菜品成功',
            data: data
        })
    })
})

// 修改菜品状态
router.post('/updatestatus',function(req,res){
    let mysql=`UPDATE menu_file SET menu_status=? WHERE menu_id=?`
    db(mysql,[req.body.foodStatus,req.body.foodId],(err,data)=>{
        if(err){
            return res.json({
                code: 500,
                msg: '修改菜品状态失败，请稍后重试'
            })
        }
        res.json({
            code: 200,
            msg: '修改菜品状态成功',
            data: data
        })
    })
})

module.exports = router